<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26
 * Time: 15:38
 */

namespace app\api\service;


use app\common\model\OrderItem;
use app\common\OrderStatus;

class Order
{
    static function list_($param)
    {
        $c = new \app\common\model\Order();
        $c->field("shop_sellers.*,shop_sellers.user_id as seller_user_id");
        $c->field("shop_order.*");
        $c->join("shop_sellers", "shop_sellers.id = shop_order.seller_id");
        $c->where(["shop_order.order_status_id" => [">", OrderStatus::DAIZHIFU]]); //待支付
        $r_data = $c->where($param)->order("shop_order.id desc")->paginate();
        foreach ($r_data as $k => $v) {
            $r_data[$k]->item = OrderItem::all(["order_id" => $v->order_id]);
            /**  售后订单信息 */
            if ($v->order_status_id == OrderStatus::BEGIN_AFTER_SALE) {
                $r_data[$k]->after_sale = AfterSale::by_order($v);
            }
//            /**  可以确认收货  */
//            if ($v->order_status_id == OrderStatus::DAIYUNSHU) {
//                $r_data[$k]->yunxu_queren_shouhuo = true;
//            }
            /**  可以申请售后 */
            if ($v->order_status_id == OrderStatus::QUERENSHOUHUO) {
                $r_data[$k]->yunxu_after_sale = true;
            }
            /**  可以取消订单 */
            if ($v->order_status_id < OrderStatus::YIJIEDAI) {
                $r_data[$k]->yunxu_cancel_order = true;
            }
            /**  允许接订单 */
            if ($v->order_status_id == OrderStatus::YIZHIFU) {
                $r_data[$k]->yunxu_jiedan = true;
            }
            /**  可以确认发货 */
            if ($v->order_status_id == OrderStatus::YIJIEDAI) {
                $r_data[$k]->yunxu_querenshouhuo = true;
            }
        }
        return $r_data;
    }

    static function order($param)
    {
        $c = new \app\common\model\Order();
        $c->field("shop_sellers.*,shop_sellers.user_id as seller_user_id");
        $c->field("shop_order.*");
        $c->join("shop_sellers", "shop_sellers.id = shop_order.seller_id");
        $r_data = $c->where($param)->find();
        if (empty($r_data)) {
            return false;
        }
        $c->item = OrderItem::all(["order_id" => $r_data->order_id]);
        return $r_data;
    }
}